package ar.Location.UI;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import ar.Location.Constant.Define;
import ar.Location.Internet.NetworkStateReceiver;
import ar.Location.PlaceObject.PlaceCategory;
import ar.activity.R;

public class DataSourceItemView extends LinearLayout {
	Context itemContext = null;
	TextView txtViewName = null;
	TextView txtViewDescription = null;
	TextView txtViewNumsOfResults = null;
	ImageView imgViewIcon = null;
	ProgressBar pbLoading = null;
	String[] strArrays = null;

	public DataSourceItemView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initContext(context);
		initView();
	}

	private void initContext(Context context) {
		// TODO Auto-generated method stub
		itemContext = context;
		strArrays = itemContext.getResources().getStringArray(R.array.data_source_description_array);
	}

	private void initView() {
		inflate(itemContext, R.layout.data_source_item, this);

		txtViewName = (TextView) findViewById(R.id.tvDataSourcesMain);
		txtViewDescription = (TextView) findViewById(R.id.tvDataSourcesSub);
		txtViewNumsOfResults = (TextView) findViewById(R.id.txtViewNumsOfResult);
		imgViewIcon = (ImageView) findViewById(R.id.ivDataSources);
		pbLoading = (ProgressBar) findViewById(R.id.progressBarDataSource);
	}

	public void bindData(PlaceCategory dataSource) {
		// TODO Auto-generated method stub

		if (dataSource != null) {
			if (dataSource.isFinishLoading()) {

				int nResults = dataSource.count();
				String count = String.valueOf(nResults);
				imgViewIcon.setAlpha(255);
				pbLoading.setVisibility(View.GONE);
				txtViewNumsOfResults.setVisibility(View.VISIBLE);
				txtViewNumsOfResults.setText(count + " items");
			} else {
				imgViewIcon.setAlpha(96);
				if (NetworkStateReceiver.isNetworkConnected) {
					pbLoading.setVisibility(View.VISIBLE);
				} else {
					pbLoading.setVisibility(View.GONE);
				}
				txtViewNumsOfResults.setVisibility(View.GONE);
			}

			if (txtViewName != null) {
				txtViewName.setText(dataSource.categoryName);
			}

			if (txtViewDescription != null) {
				txtViewDescription
						.setText(strArrays[dataSource.categoryIndex]);
			}

			if (imgViewIcon != null) {
				imgViewIcon
						.setImageResource(Define.listDataSourceIcons[dataSource.categoryIndex]);
			}
		}
	}
}
